<?php

namespace app\common\logic;

use app\common\model\trResource;
use app\common\model\trSystemSet;
use app\common\util\Tools;

/**
 * Class Auth
 * @package app\web\service
 * 固定路线逻辑类
 */
class BusesLogic
{
    // 判断路线是否符合设定的 1：日期；2：周末；3：默认
    public static function getJudgeOpenType($data = [], $date = 0){
        if ($data["openDate"] && !empty($data["dateBetween"])) { // 开启了日期
            $data["dateBetween"] = explode(",", $data["dateBetween"]);
            if ($data["dateBetween"][0] <= $date && $date <= $data["dateBetween"][1]) {
                return 1; // 代表 日期
            }
        }elseif ($data["openWeekend"] && (date('w', strtotime($date)) == 6 || date('w', strtotime($date)) == 0)) { // 开启了周末
            return 2; // 代表 周末
        }elseif ($data["openDefault"]) { // 开启了默认
            return 3;
        }else{
            return $data["defaultAdultPrice"] ? 3 : ($data["dateAdultPrice"] ? 1 : 2);
        }
        return 0;
    }

    // 小程序的线路地址展示，显示2级，后面优先
    public static function getShowAddress($data){
        $name = explode(" ", Tools::getNameByPCA($data["provinceCodeFrom"], $data["cityCodeFrom"], $data["areaCodeFrom"]));
        $addressFrom = (isset($name[2]) ? $name[2] : $name[1]) . "-{$data["addressFrom"]}";
        $name = explode(" ", Tools::getNameByPCA($data["provinceCodeTo"], $data["cityCodeTo"], $data["areaCodeTo"]));
        $addressTo = (isset($name[2]) ? $name[2] : $name[1]) . "-{$data["addressTo"]}";
        return [$addressFrom, $addressTo];
    }

    // 获取页面展示的成人线路价格
    public static function getShowPrice($data, $type = 0){ // 1：日期；2：周末；3：默认
        $price = 0;
        switch ($type){
            case "1":
                $price = $data["dateAdultPrice"];
                break;
            case "2":
                $price = $data["weekendAdultPrice"];
                break;
            case "3":
                $price = $data["defaultAdultPrice"];
                break;
            default:
                break;
        }
        return $price;
    }

    // 获取页面展示的儿童线路价格
    public static function getShowCPrice($data, $type = 0){ // 1：日期；2：周末；3：默认
        $price = 0;
        switch ($type){
            case "1":
                $price = $data["dateChildrenPrice"];
                break;
            case "2":
                $price = $data["weekendChildrenPrice"];
                break;
            case "3":
                $price = $data["defaultChildrenPrice"];
                break;
            default:
                break;
        }
        return $price;
    }

    // 获取页面展示的佣金
    public static function getShowCommission($data, $type = 0){ // 1：日期；2：周末；3：默认
        $price = 0;
        switch ($type){
            case "1":
                $price = $data["dateCommission"];
                break;
            case "2":
                $price = $data["weekendCommission"];
                break;
            case "3":
                $price = $data["defaultCommission"];
                break;
            default:
                break;
        }
        return $price;
    }

    public static function getLineRemark(){
        $lineRemarkData = trSystemSet::getValue("enterpriseRemarks");
        if(empty($lineRemarkData)){
            $lineRemarkData = [
                "enterpriseRemarks" => "",   //巴士线路备注
                "enterpriseAlias" => "巴士线路",   //巴士线路备注
                "hotLineBanner" => "/attachment/default/hotline-1.jpg",
                "hotLineRemark" => "热门线路"
            ];
            $lineRemarkData["hotLineBannerUrl"] = tome_dia($lineRemarkData["hotLineBanner"]);
        }else{
            $lineRemarkData["hotLineBannerUrl"] = tome_dia(trResource::getValue("url", ["id" => $lineRemarkData["hotLineBanner"]]));
        }
        return $lineRemarkData;
    }

    public static function getTimeRule($time){
        if (strlen($time) <= 16) { // 格式：2019-05-15 08:00
            $rule = "H:i";
        } else { // 格式：2019-05-15 08:23:33
            $rule = "H:i:s";
        }
        return $rule;
    }

    // 获取线路的班次数据结构
    public static function getClassesTimeScope($data, $s = null){
        $long = 30; // 时间长度，默认30天 ，可预定在30天内
        if(is_numeric($s)){
            $s = $s ? $long - $s : $long;
        }else{
            $s = 0;
        }
        $scope = $res = [];
        if($s < $long) {
            $data["operationTime"] = array_map(function ($v) {
                return date('Y-m-d') . " " . $v;
            }, explode(",", $data["operationTime"])); // 运营时间
            $rule = self::getTimeRule($data["operationTime"][0]);
            $time2 = strtotime($data["operationTime"][1]);
            $time1 = strtotime($data["operationTime"][0]);
            $timestamp = ($time2 - $time1) / 60;
            $shifts = floor($timestamp / $data["shifts"]); // 根据运营时间计算一天有多少次车
            for ($j = 0; $j <= $shifts; $j++) {
                $k = date($rule, $time1 + $j * $data["shifts"] * 60);
                $res[$j] = [
                    "shift" => $k,
                    "ticketNumber" => $data["ticketNumber"], // 总票
                    "standbyTicket" => $data["ticketNumber"], // 余票
                    "deferTicket" => 0, // 延期余票（等待付款）
                ];
            }
            for ($i = $s; $i < $long; $i++) { // 时间长度，默认30天
                $k = date('Y-m-d', strtotime("-0 year -0 month +$i day"));
                $scope[$k] = $res;
            }
        }
        return $scope;
    }

    // 获取线路的班次数据数量
    public static function getClassesTimeCount($operationTime, $shifts){
        if(is_string($operationTime)) {
            $operationTime = array_map(function ($v) {
                return date('Y-m-d') . " " . $v;
            }, explode(",", $operationTime));
        }
        $timestamp = (strtotime($operationTime[1]) - strtotime($operationTime[0])) / 60;
        $shifts = floor($timestamp / $shifts); // 根据运营时间计算一天有多少次车
        return ++$shifts;
    }
}